import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { forEach } from '@angular/router/src/utils/collection';

@Component({
  selector: 'app-recommend-datum',
  templateUrl: './recommend-datum.component.html',
  styleUrls: ['./recommend-datum.component.scss'],
})
export class RecommendDatumComponent implements OnInit {
  @Input() tagData;
  listdata: any;
  constructor(private appGlobalService: AppGlobalService) { }

  ngOnInit() {
    /* this.tagData = {
      "type": "tag",
      "style": "fenceGridTagTag",
      "name": null,
      "linearLayout": "body",
      "data": {
        "rightWay": {
          "title": "话术资料",
          "items": [
            {
              "iconImage": "../../../assets/img/xing.png",
              "style": "",
              "link": {
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              }
            },
            {
              "iconImage": "../../../assets/img/xing.png",
              "style": "",
              "link": {
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              }
            },
            {
              "iconImage": "../../../assets/img/xing.png",
              "style": "",
              "link": {
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              }
            },
            {
              "iconImage": "../../../assets/img/xing.png",
              "style": "",
              "link": {
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              }
            }
          ]
        },
        "leftWay": {
          "buttons": [
            {
              "link": {
                "label": "我的推荐",
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              },
              "style": "background: rgba(255,133,63,0.15);margin-right: 5px;color: rgba(255,99,20,1);",
              "label": "我的推荐"
            },
            {
              "link": {
                "label": "确认成交",
                "type": "page",
                "params": {
                  "aaa": "1"
                },
                "target": "157676"
              },
              "style": "background: rgba(108,203,89,0.15); margin-left: 5px;color:#48BF30;",
              "label": "确认成交"
            }
          ],
          "iconImage": "../../../assets/img/home_recommend.png",
          "tip": "<font style='color: rgba(133,136,148,1)'>人工推荐<font style='color:#FF6314'>10%</font> ,系统<font style='color:#FF6314'>5%</font></font> ",
          "title": "推荐有奖"
        }
      }
    } */
    /* // 插入html代码块 */
    this.listdata = this.tagData.data;
    const div = document.createElement('div');
    div.innerHTML = this.listdata.leftWay.tip; // 设置显示的数据，可以是标
    // 动态插入到body中
    const jiedian = document.getElementById('text');
    jiedian.insertBefore(div, jiedian.lastChild);
    /* 重组item数据,添加cssStrs为背景色 */
    this.listdata.rightWay.items.forEach(element => {
      if (element.style && element.style !== "") {
        const cssStyles = element.style.split(";");
        cssStyles.map(v => {
          const cssStrs = v.split(":");
          element.cssStr = {};
          element.cssStr[cssStrs[0]] = cssStrs[1];
        });
      } else {
        element.cssStr = {};
      }
    });
  }
  // 按钮跳转
  go(obj) {
    if (obj) {
      const pageParams = {
        pageId: obj.target,
        params: obj.params,
        sessionId: localStorage.getItem('sessionId')
      };
      console.log(pageParams);
      this.appGlobalService.goToForward(pageParams);
    }
  }

  getNgStyle(str) {
    try {
      return JSON.parse(str);
    } catch (error) {
      return {};
    }
  }
}
